// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2

package file

import (
	"net/http"

	"nexus/dto"
	"nexus/internal/logic/file"
	"nexus/internal/svc"
	"nexus/internal/types"

	"github.com/zeromicro/go-zero/rest/httpx"
)

func FilePreviewHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.FilePreviewReq
		if err := httpx.Parse(r, &req); err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
			return
		}

		l := file.NewFilePreviewLogic(r.Context(), svcCtx)
		resp, err := l.FilePreview(&req)
		if err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
		} else {
			// httpx.OkJsonCtx(r.Context(), w, resp)
			url := "http://192.168.110.75/static" + resp.Data.(*dto.File).Path
			// 重定向到文件预览地址
			http.Redirect(w, r, url, http.StatusFound)
		}
	}
}
